package com.foxzzz.two.pointers;

public class Solution977 {
    public int[] sortedSquares(int[] A) {
        if (A.length == 0) {
            return new int[0];
        }
        if(A.length==1){
            return new int[]{A[0]*A[0]};
        }
        int low = 0;
        int high = A.length - 1;
        int res[] = new int[A.length];
        int index = A.length - 1;
        while (low < high) {
            int highSqure = A[high] * A[high];
            int lowSqure = A[low] * A[low];
            if (highSqure > lowSqure) {
                res[index] = highSqure;
                high--;
                //处理最后一个数
                if(low>=high){
                    res[0] = lowSqure;
                }
            } else {
                res[index] = lowSqure;
                low++;
                //处理最后一个数
                if(low>=high){
                    res[0] = highSqure;
                }
            }

            index--;
        }
        return res;
    }

    public static void main(String[] args) {
        new Solution977().sortedSquares(new int[]{-7,-3,2,3,11});
        new Solution977().sortedSquares(new int[]{-4});
        new Solution977().sortedSquares(new int[]{-4,-1,0,3,10});
    }
}
